package com.hex.ds.hdtp.core.web.script.test.controller;

import com.hex.bigdata.hbda.annotation.Log;
import com.hex.ds.hdtp.core.app.common.page.dto.PageDto;
import com.hex.ds.hdtp.core.app.script.test.dto.request.ScriptDataTestResultRequestDto;
import com.hex.ds.hdtp.core.app.script.test.service.IScriptDataTestResultService;
import com.hex.ds.hdtp.core.web.common.page.PageVo;
import com.hex.ds.hdtp.core.web.common.page.converter.IPageVoAndPageDtoConverter;
import com.hex.ds.hdtp.core.web.script.test.converter.IScriptDataTestResultVoConverter;
import com.hex.ds.hdtp.core.web.script.test.vo.query.ScriptDataTestResultQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.nio.file.Files;

@Slf4j
@RestController
@Api(tags = "04-脚本测试：测试结果")
@RequestMapping("/api/script/data/test/result")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ScriptDataTestResultController {

    private final IScriptDataTestResultService scriptDataTestResultService;

    private final IScriptDataTestResultVoConverter converter;

    private final IPageVoAndPageDtoConverter pageVoAndPageDtoConverter;

    /*
     * @Method: queryListByPage <br>
     * @Param: [scriptDataTestResultQueryVo, pageVo] <br>
     * @Return: org.springframework.http.ResponseEntity <br>
     * @Description：带数据测试结果-分页查询<br>
     * @Author： wz.li<br>
     * @Date： 2023/8/7 13:51 <br>
     * @Version： V2.0.2<br>
     */
    @GetMapping
    @ApiOperation("带数据测试结果-分页查询")
    public ResponseEntity queryListByPage(ScriptDataTestResultQueryVo scriptDataTestResultQueryVo, PageVo pageVo) {
        log.debug("进入到【ScriptDataTestResultController.queryListByPage】方法,参数：{}" + scriptDataTestResultQueryVo.toString());
        final PageDto pageDto = pageVoAndPageDtoConverter.toPageDto(pageVo);
        ScriptDataTestResultRequestDto requestDto = converter.toScriptDataTestResultRequestDto(scriptDataTestResultQueryVo);
        return ResponseEntity.ok(scriptDataTestResultService.queryListByPage(requestDto, pageDto));
    }

    /**
     * @param scriptDataTestResultQueryVo
     * @Method downloadFile
     * @Return org.springframework.http.ResponseEntity
     * @Description 带数据测试结果-下载脚本文件
     * @Author Yin.Yang
     * @Date 2023/10/20 14:20
     * @Version V1.0
     */
    @Log("下载脚本文件")
    @PostMapping(value = {"/download/file"})
    @ApiOperation("带数据测试结果-下载脚本文件")
    public ResponseEntity downloadScriptFile(@RequestBody ScriptDataTestResultQueryVo scriptDataTestResultQueryVo) throws Exception {
        log.debug("进入到【ScriptDataTestResultController.downloadScriptFile】方法,参数：{}" + scriptDataTestResultQueryVo);
        String zipFilePath = scriptDataTestResultService.downloadScriptFile(converter.toScriptDataTestResultRequestDto(scriptDataTestResultQueryVo));
        byte[] bytes = Files.readAllBytes(new File(zipFilePath).toPath());
        ByteArrayResource bar = new ByteArrayResource(bytes);
        return ResponseEntity.ok()
                .contentType(MediaType.APPLICATION_OCTET_STREAM)
                .header("Content-disposition", "attachment; filename=测试脚本和日志文件.zip")
                .body(bar);
    }

}
